//
//  EPMMarketViewModel.m
//  myXianfeng
//
//  Created by 马继鵬 on 17/5/7.
//  Copyright © 2017年 EPM. All rights reserved.
//

#import "EPMMarketViewModel.h"
#import "EPMCategoryModel.h"
#import "EPMSubCategory.h"

@interface EPMMarketViewModel()

@property (strong,nonatomic) NSDictionary* MarketDict;
@end
@implementation EPMMarketViewModel

+ (instancetype)shearedManger{
    static EPMMarketViewModel* _instance;
    static dispatch_once_t token;
    dispatch_once( &token, ^{
        _instance = [EPMMarketViewModel new];
        _instance.MarketDict = [self requestSuperMarketInfomation];
        [_instance addNotificartions];
    });
    return _instance;
}

- (void)addNotificartions{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(maketSubCatergoryDidChangedNotification:) name:EPMMaketSubCatergoryDidChangedNotification object:nil];
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(marketCatergoryDidChangedNotification:) name:EPMMarketCatergoryDidChangedNotification object:nil];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

//请求商品数据
+ (NSDictionary *)requestSuperMarketInfomation {
    NSString *jsonStr = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"market.json" ofType:@"php"] encoding:0 error:nil];
    
    NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingAllowFragments error:nil];
    return jsonDict;
}

#pragma mark - 获取分类 -

- (NSArray*)getCaterry{
    NSArray* categorys = self.MarketDict[@"data"][@"categories"];
    NSMutableArray* tempList = [NSMutableArray array];
    
    for (NSDictionary* dict in categorys){
        EPMCategoryModel* model = [EPMCategoryModel new];
        [model setValuesForKeysWithDictionary:dict];
        [tempList addObject:model];
        NSArray* arr = model.cids;
        //二层解析
        NSMutableArray* temp = [NSMutableArray array];
        for (NSDictionary* dictTemp in arr){
            EPMSubCategory* sub = [EPMSubCategory new];
            [sub setValuesForKeysWithDictionary:dictTemp];
            [temp addObject:sub];
        }
        model.cids = temp.copy;
    }
    
    return tempList.copy;
    
}

- (NSArray*)catergoryList{
    if(_catergoryList == nil){
        _catergoryList = [self getCaterry];
        [self getGoods];
    }
    return _catergoryList;
}

#pragma mark - 商品列表转化 -

- (void)getGoods{
    NSDictionary* dict = self.MarketDict[@"data"][@"products"];
    
    for (EPMCategoryModel* model in _catergoryList){
        NSArray* temp = dict[[NSString stringWithFormat:@"%@",model.id]];
        NSMutableArray* mutable = [NSMutableArray array];
        for (NSDictionary* dictTemp in temp){
            EPMGoodsModel* good = [EPMGoodsModel new];
            [good setValuesForKeysWithDictionary:dictTemp];
            [mutable addObject:good];
        }
        
        model.goodList = mutable.copy;
    }
    
    }

#pragma mark -  -

- (void)maketSubCatergoryDidChangedNotification:(NSNotification*)noti{
    
    NSInteger cid = [noti.userInfo[KSubCatergoryChanged] integerValue];
    NSInteger catergortIndex = [noti.userInfo[KCatergoryChanged] integerValue];
    EPMCategoryModel* model = self.catergoryList[catergortIndex];

    
    NSArray* goodList = model.goodList;
    NSMutableArray* temp = [NSMutableArray array];
    
    for (EPMGoodsModel* good in goodList){
        NSInteger goodCid = [good.child_cid intValue];
        if ( cid == goodCid){
            [temp addObject:good];
        }
    }
    
    if(self.marketDisPlayListBlock){
        self.marketDisPlayListBlock(temp.copy);
    }
}
#pragma mark - 数据接收 -

- (void)marketCatergoryDidChangedNotification:(NSNotification*)noti{
    NSInteger index = [noti.userInfo[KCatergoryChanged] integerValue];
    EPMCategoryModel* model = _MarketManger.catergoryList[index];
    
    
    if(self.marketDisPlayListBlock){
        self.marketDisPlayListBlock(model.goodList);
    }
}

@end
